import java.util.ArrayList;

import processing.core.PConstants;
import controlP5.ControlP5;

//Artist View is seen within Top View
//	Used for displaying Artist info
//		+ Artist suggestions

public class ArtistView extends View {
	private ArtistData theArtist;
	ControlP5 controlP5;
	
	controlP5.Button B_Ignore;
	
	ArrayList<Integer> similarIndex;
	
	public ArtistView(int _x1, int _y1, int _x2, int _y2, ArtistData a) {
		x1 = _x1;
		y1 = _y1;
		x2 = _x2;
		y2 = _y2;
		theArtist = a;
		
		setup();
	}
	
	//Create Buttons & Such
	//	Call at end of all constructors
	private void setup() {
		controlP5 = new ControlP5(GLOBAL.processing);
			controlP5.setAutoDraw(false);
			
		B_Ignore = controlP5.addButton("vw_ignore", 0, x1-60, y1-60, 65, 25);
			B_Ignore.setLabel("Ignore Artist");
			GLOBAL.setButtonColors(B_Ignore);
			
		findSimilar();
	}
	
	public void resize(int _x1, int _y1, int _x2, int _y2) {
		x1 = _x1;
		y1 = _y1;
		x2 = _x2;
		y2 = _y2;
		
		width = x2 - x1;
		height = y2 - y1;
		
		B_Ignore.setPosition(x1-60, y1-60);
	}
	
	public ArtistData getArtist() {
		return theArtist;
	}
	
	public void draw() {
		controlP5.draw();
		GLOBAL.processing.fill(GLOBAL.processing.color(200, 230, 235));
		GLOBAL.processing.rect(x2+12, y1+20, 200, 250);
		
		GLOBAL.processing.fill(0);
		
		GLOBAL.processing.text("Gender (%)", x1, y2-40);
		GLOBAL.processing.text("Top 3 Countries", x1+278, y2-40);
		
		GLOBAL.processing.textSize(16);
		GLOBAL.processing.text("Similar Artists", x2+15, y1+40);
		GLOBAL.processing.text("Top Tracks", x2+15, y1+155);
		
		GLOBAL.processing.textAlign(PConstants.CENTER);
		GLOBAL.processing.fill(GLOBAL.processing.color(113, 12, 66));
		GLOBAL.processing.text(theArtist.getName(), ((x1+x2)/2)+12, y1-15);
		
		GLOBAL.processing.fill(0);
		GLOBAL.processing.textSize(14);
		GLOBAL.processing.text("-- Who's Listening? --", x1+180, y2-40);
		
		GLOBAL.processing.textSize(12);
		GLOBAL.processing.text(theArtist.theBreakdown.getArtistInfo(),((x1+x2)/2)+12, y1+5);

		GLOBAL.processing.textAlign(PConstants.LEFT);
		drawGenders();
		drawCountries();
		drawSimilar();
		drawTracks();
		GLOBAL.processing.noStroke();
	}
	
	private void drawGenders(){
		int pFemale = theArtist.theBreakdown.getPercentFemale();
		int pMale = theArtist.theBreakdown.getPercentMale();
		
		GLOBAL.processing.stroke(100);
		GLOBAL.processing.fill(GLOBAL.colorFemale);
		GLOBAL.processing.image(GLOBAL.processing.loadImage("female.gif"), x1, y2-29);
		GLOBAL.processing.rect(x1+20, y2-29, pFemale, 12);
		
		GLOBAL.processing.fill(GLOBAL.colorMale);
		GLOBAL.processing.image(GLOBAL.processing.loadImage("male.gif"), x1, y2-12);
		GLOBAL.processing.rect(x1+20, y2-12, pMale, 12);
		
		GLOBAL.processing.fill(0);
		GLOBAL.processing.text("" + pFemale + "%", x1+25+pFemale, y2-18);
		GLOBAL.processing.text("" + pMale + "%", x1+25+pMale, y2-2);
	}
	
	private void drawCountries() {
		int c1 = theArtist.theBreakdown.getPercentTopCountry(0);
		int c2 = theArtist.theBreakdown.getPercentTopCountry(1);
		int c3 = theArtist.theBreakdown.getPercentTopCountry(2);
		
		GLOBAL.processing.fill(GLOBAL.colorCountry);
		GLOBAL.processing.rect(x1+300, y2-29, c1, 12);
		GLOBAL.processing.rect(x1+300, y2-12, c2, 12);
		GLOBAL.processing.rect(x1+300, y2+5, c3, 12);
		
		GLOBAL.processing.fill(0);
		GLOBAL.processing.text("" + c1 + "%  " + theArtist.theBreakdown.getTopCountry(0), x1+305+c1, y2-18);
		GLOBAL.processing.text("" + c2 + "%  " + theArtist.theBreakdown.getTopCountry(1), x1+305+c2, y2-2);
		GLOBAL.processing.text("" + c3 + "%  " + theArtist.theBreakdown.getTopCountry(2), x1+305+c3, y2+16);
		
		GLOBAL.processing.image(theArtist.theBreakdown.getTopCountryImage(0), x1+278, y2-29);
		GLOBAL.processing.image(theArtist.theBreakdown.getTopCountryImage(1), x1+278, y2-12);
		GLOBAL.processing.image(theArtist.theBreakdown.getTopCountryImage(2), x1+278, y2+5);
	}
	
	private void drawSimilar() {
		int yOffset = 60;
		GLOBAL.processing.line(x2+12, y1+45, x2+120, y1+45);
		GLOBAL.processing.textSize(12);
		for(int x=0; x<similarIndex.size(); ++x) {
			
			String name = radioGaGa.ArtistList.get(similarIndex.get(x)).getName();
			if(name.length() > 19) 
				name = name.substring(0, 19) + "...";

			GLOBAL.processing.text(name, x2+15, y1+yOffset);
			yOffset += 16;
		}
	}
	
	private void drawTracks() {
		int yOffset = 175;
		GLOBAL.processing.line(x2+12, y1+160, x2+120, y1+160);
		GLOBAL.processing.textSize(12);
		for(int x=0; x<5; ++x) {
			GLOBAL.processing.text(theArtist.theBreakdown.getTopTrack(x), x2+15, y1+yOffset);
			yOffset += 16;
		}
	}
	
	private void findSimilar() {
		similarIndex = new ArrayList<Integer>();
		
		for(int x=0; x<5; ++x) {
			int temp = theArtist.theBreakdown.getSimilarArtist(x);
			
			if(temp > 0)
				similarIndex.add(temp);
		}
		
	}
	
}
